package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;
import org.json.JSONObject;

import DataStorageClasses.Reviewer;
import Database.DatabaseConnector;

/**
 * Servlet implementation class SubLogin
 */
@WebServlet("/SubLogin")
public class SubLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("In sub login servlet");
		HttpSession session = request.getSession();
		 DatabaseConnector db = new DatabaseConnector();
		 String userName = request.getParameter("userName");
		 String password = request.getParameter("password"); 
		 String email = request.getParameter("email"); 
		 
		 System.out.println("sublogin usernam: " + userName);
		 System.out.println("sublogin password: " + password);
		 
		 JSONObject subInfo = new JSONObject();
		 
		if( db.authenticateReviewer(userName, password))
		{
			 session.setAttribute("subloggedIn", true);
			 session.setAttribute("subUser", userName);
			 session.setAttribute("subPass", password);
			 session.setAttribute("subEmail", email);

			 try {
				subInfo.put("SubUserName", userName);
				subInfo.put("SubPass", password);
				subInfo.put("SubEmail", email);
				
				
				response.getWriter().print(subInfo.toString());
				response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
			 } 
			 catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
			 }
		}
		else
		{
			System.out.println("You Fail!");
			response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
		}
		
	}

}
